package cn.edu.cqvie.atc.loop;

/**
 * 三角形数列
 * 描述：该数列中的第n项是由第n-1项n等到的
 * (递归包含，递，和归)
 *
 * @author Zheng Shaohong
 */
public class Triangle {
    public static void main(String[] args) {
        System.out.println(getNumberByRecursion(1));
        System.out.println(getNumberByRecursion(2));
        System.out.println(getNumberByRecursion(3));
        System.out.println(getNumberByRecursion(4));
        System.out.println(getNumberByRecursion(5));
    }

    public static int getNumber(int n) {
        int total = 0;
        while (n > 0) {
            total += n;
            n--;
        }
        return total;
    }

    public static int getNumberByRecursion(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n + getNumberByRecursion(n - 1);
        }
    }
}
